package com.forever.controller;

import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.ChatMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.model.output.Response;
import dev.langchain4j.model.output.TokenUsage;

import java.time.Duration;
import java.util.List;

import static dev.langchain4j.data.message.UserMessage.userMessage;


public class OpenAiOllama {

    public static void main(String[] args) {

        Duration duration = Duration.ofSeconds(200);
        ChatLanguageModel model =
                new OpenAiChatModel
                .OpenAiChatModelBuilder()
                .baseUrl("http://localhost:11434/v1")
                .apiKey("ollama")
                .modelName("qwen2:7b")
                .logRequests(true)
                .logResponses(true)
                .timeout(duration)
                .build();
        UserMessage userMessage = userMessage("说一下java优秀的三个点?");
        Response<AiMessage> response = model.generate(userMessage);
        System.out.println("response:");
        System.out.println(response);


        TokenUsage tokenUsage = response.tokenUsage();

        System.out.println("tokenUsage:");
        System.out.println(tokenUsage);
        List<ChatMessage> messages = List.of(new AiMessage("说一下java优秀的三个点?"));
        System.out.println("messages.content:");
        System.out.println(model.generate(messages).content());

    }
}
